package com.guanlin.notification;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import com.guanlin.notification.notification.NotificationApiCompat;
import com.guanlin.notification.notification.NotificationConstants;
import com.guanlin.notification.notification.NotificationUtils;
import com.guanlin.notification.protect.ProtectService;


/**
 * 1.提醒用户 开启白名单
 * 2.提醒用户 开启自启动
 * 3.厂商通道的集成
 * 4.设防问题
 *
 *  * 自启动：可以监听到开关机并拉起应用进程 或者 其他应用可以拉起
 *  * <p>
 *  * 白名单：加了白名单，休眠状态下不会被杀死，但是因为各大厂商对系统的修改，需要做针对性的处理
 *  * ，不然在有的机型上还是会被杀掉
 *  * https://mp.weixin.qq.com/s/Eh-OOqZ_WuKj4fmB_czS_w自启动：可以监听到开关机并拉起应用进程
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void show(View view) {
        Log.i("ProtectService", "show: ");
        ProtectService.startSelf(this);
        utils();
        normal();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void normal() {
        NotificationChannel notificationChannel = new NotificationChannel(
                NotificationConstants.NOTIFICATION_CHANNEL_ID_DEFAULT
                , NotificationConstants.NOTIFICATION_CHANNEL_NAME_DEFAULT
                , NotificationManager.IMPORTANCE_DEFAULT);
        NotificationUtils notificationUtils = new NotificationUtils(this);
        notificationUtils.getManager().createNotificationChannel(notificationChannel);
        Notification notification = new Notification.Builder(this, NotificationConstants.NOTIFICATION_CHANNEL_ID_DEFAULT)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("默认标题")
                .setContentText("默认内容")
                .setContentIntent(getPedingIntent())
                .setAutoCancel(true)
                .build();
        notificationUtils.getManager().notify(12, notification);
    }

    private void utils() {
        NotificationApiCompat notificationApiCompat =
                new NotificationApiCompat.Builder(this
                        , NotificationConstants.NOTIFICATION_CHANNEL_ID_DEFAULT
                        , NotificationConstants.NOTIFICATION_CHANNEL_NAME_DEFAULT
                        , R.mipmap.ic_launcher)
//                        .setContentTitle("默认标题")
//                        .setContentText("默认内容")
//                        .setContentIntent(getPedingIntent())
                        .setContentView(getRemoteViews())
                        .setAutoCancel(true)
                        .build();
        notificationApiCompat.notify(12);
    }


    private RemoteViews getRemoteViews() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_mobile_play);
        // 设置 点击通知栏的上一首按钮时要执行的意图
//        remoteViews.setOnClickPendingIntent(R.id.btn_pre, getActivityPendingIntent(11));
//        // 设置 点击通知栏的下一首按钮时要执行的意图
//        remoteViews.setOnClickPendingIntent(R.id.btn_next, getActivityPendingIntent(12));
//        // 设置 点击通知栏的播放暂停按钮时要执行的意图
//        remoteViews.setOnClickPendingIntent(R.id.btn_start, getActivityPendingIntent(13));
//        // 设置 点击通知栏的根容器时要执行的意图
//        remoteViews.setOnClickPendingIntent(R.id.ll_root, getActivityPendingIntent(14));
        remoteViews.setTextViewText(R.id.tv_title, "标题");     // 设置通知栏上标题
        remoteViews.setTextViewText(R.id.tv_artist, "艺术家");   // 设置通知栏上艺术家
        return remoteViews;
    }


    private PendingIntent getPedingIntent() {
        Intent intent = new Intent(getApplication(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplication()
                , 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }
}
